﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TLM.VsmdiReader
{
    /// <summary>
    /// This class represents a test list in a vsmdi file
    /// </summary>
    public class TestList
    {
        /// <summary>
        /// Constructor for TesList
        /// </summary>
        public TestList()
        {
            ChildLists = new List<TestList>();
            TestLinks = new List<TestLink>();
        }

        /// <summary>
        /// Gets or sets the id of the testlist
        /// </summary>
        public Guid Id { get; set; }
        /// <summary>
        /// The Name of the testlist
        /// </summary>
        /// <example>Frontends</example>
        /// <remarks>Not unique within the complete test list hierarchy</remarks>
        public string Name  { get; set; }
        /// <summary>
        /// Current sub testlists. Subnodes within the tree.
        /// </summary>
        public IEnumerable<TestList> ChildLists  { get; set; }
        /// <summary>
        /// The tests contained on this node level
        /// </summary>
        public IEnumerable<TestLink> TestLinks  { get; set; }
        /// <summary>
        /// Is the current testlist enabled or not
        /// </summary>
        public bool Enabled { get; set; }

        /// <summary>
        /// Gets all the test links that are in a test list. 
        /// This function is recursive so it will also get all the test links from the child lists.
        /// </summary>
        /// <returns>A list of test links containing all test links from the test list and its child lists</returns>
        public IEnumerable<TestLink> GetAllTestLinks()
        {
            // This would be better if we did it bottom-up, but initial testing didn't show any
            // performance issues. This implementation can be changed if performance becomes an issue.
            List<TestLink> allTestLinks = TestLinks.ToList();
            foreach (TestList childList in ChildLists)
            {
                if (childList.Enabled)
                {
                    allTestLinks.AddRange(childList.GetAllTestLinks());
                }
            }
            return allTestLinks;
        }
    }
}